/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.exam;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * <p>Project: javase - Wx
 * <p>Powered by webrx On 2021-12-10 09:30:51
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Wx extends JFrame implements ActionListener, KeyListener {
    JTextField txt;
    JLabel info;
    Font font = new Font("宋体", 0, 25);
    String ttt = "Hello java php public extends JFrame class label implements Font";
    long start = 1;
    long end = 0;

    public Wx() {
        super();
        setLayout(null);
        setSize(900, 500);
        info = new JLabel(setInfo(ttt, ""));
        info.setBounds(10, 10, 900, 45);
        info.setFont(font);
        add(info);

        txt = new JTextField(900);
        txt.setBounds(10, 55, 850, 45);
        txt.setFont(font);
        txt.addKeyListener(this);
        add(txt);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(3);
        setVisible(true);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    public String setInfo(String str, String rsu) {
        String end = str.substring(rsu.length());
        StringBuilder sss = new StringBuilder("<html>");
        for (int i = 0; i < rsu.length(); i++) {
            String s1 = rsu.substring(i, i + 1);
            String s2 = str.substring(i, i + 1);
            if (s1.equals(s2)) {
                sss.append(String.format("<span style=color:green>%s</span>", s2));
            } else {
                sss.append(String.format("<span style=background:red>%s</span>", s2));
            }
        }
        sss.append(String.format("<span style=color:gray>%s</span>", end));
        sss.append("</html>");
        return sss.toString();
    }


    @Override
    public void keyReleased(KeyEvent e) {
        if (start == 1) {
            start = System.currentTimeMillis();
        } else {
            this.setTitle("打字速度：" + (ttt.length() / ((System.currentTimeMillis() - start * 1.0) / 1000)) * 60 + "字/分");
        }
        if (txt.getText().length() < ttt.length()) {
            info.setText(setInfo(ttt, txt.getText()));
        } else {
            end = System.currentTimeMillis();
            JOptionPane.showMessageDialog(this, "打字速度：" + (ttt.length() / ((end - start) / 1000)) * 60 + "字/分", "系统提示：" + ttt.length() + "字", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public void Wxa() {
        //super("webrx");
        setSize(900, 600);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(3);
        JButton n = new JButton("北");
        JButton s = new JButton("南");
        JButton w = new JButton("西");
        JButton e = new JButton("东");
        add(n, "North");
        add(s, BorderLayout.SOUTH);
        add(w, BorderLayout.WEST);
        add(e, BorderLayout.EAST);
        //JButton c = new JButton("中央");
        //add(c);
        //c.addActionListener(this);
        //JPanel panel = new JPanel(new FlowLayout(1, 0, 0));
        GridLayout gl = new GridLayout(0, 1, 0, 5);
        JPanel panel = new JPanel(gl);
        JTextField txt = new JTextField();
        panel.add(new JLabel("<font color=red>hello</font>java swing....."));
        panel.add(new JTextField());
        panel.add(new JTextField());
        panel.add(new JTextField());
        panel.add(new JTextField());


        add(panel, "Center");
        n.addActionListener(this);
        s.addActionListener(this);
        w.addActionListener(this);
        e.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
    }

    public static void main(String[] args) {
        new Wx();
    }
}
